package com.atguigu.lease.web.admin.custom.converter;

import com.atguigu.lease.model.enums.ItemType;
import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;

/**
 * 自定义转换器: String -> ItemType
 */
//@Component
public class StringToItemTypeConverter implements Converter<String, ItemType> {

    /**
     * 类型转换
     *
     * @param source
     * @return
     */
    @Override
    public ItemType convert(String source) {
        for (ItemType itemType : ItemType.values()) {
            if (source.equals(itemType.getCode() + "")) {
                return itemType;
            }
        }

        // 主动抛出异常
        throw new RuntimeException("传入参数类型错误！必须是1或2");
    }
}
